<?php

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}


/**
 * 获取用户中心默认页面所需的数据
 *
 * @access  public
 * @param   int         $user_id            用户ID
 *
 * @return  array       $info               默认页面所需资料数组
 */
function get_user($user_id)
{
	
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('users'). " AS u JOIN ". $GLOBALS['ecs']->table('gender'). " AS g "." WHERE user_id = '$user_id' AND u.sex = g.xid ";
    $row = $GLOBALS['db']->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('users'). " AS u JOIN ". $GLOBALS['ecs']->table('trainingservice'). " AS t JOIN ". $GLOBALS['ecs']->table('experience')." AS e "." WHERE user_id = '$user_id' AND t.xid = u.training_service AND u.experience=e.xid ";
    $tmp = $GLOBALS['db']->getRow($sql);
	
    $info = array();
    $info['username']  = stripslashes($row['user_name']);
	$info['user_type']  = $row['user_type'];
	$info['user_id']  = $row['user_id'];
   
    //如果$_SESSION中时间无效说明用户是第一次登录。取当前登录时间。
    $last_time = !isset($_SESSION['last_time']) ? $row['last_login'] : $_SESSION['last_time'];

    if ($last_time == 0)
    {
        $_SESSION['last_time'] = $last_time = gmtime();
    }
	
    $info['last_time'] 		  = local_date($GLOBALS['_CFG']['time_format'], $last_time);
	$info['firstname'] 		  = $row['firstname'];
	$info['lastname']         = $row['lastname'];
	$info['user_money']		  = $row['user_money'];	
	$info['address'] 	      = $row['address'];
	$info['province'] 		  = $row['province'];
	$info['country'] 		  = $row['country'];
	$info['gender']			  = $row['sex'];
	$info['genderType'] 	  = $row['genderType'];
	$info['photo']            = $row['photo'];
	$info['credit_line']      = $row['credit_line'];
	$info['experienceType']   = $tmp['experienceType'];
	$info['experience']       = $tmp['experience'];
	$info['training_specialties'] = $row['training_specialties'];
	$info['trainingServiceType'] = $tmp['trainingServiceType'];
	$info['trainingService']  = $tmp['trainingService'];
	$info['credentials']	  = $row['credentials'];
	$info['BIO']              = $row['BIO'];
	$info['education']        = $row['education'];
	$info['school']			  = $row['school'];
	$info['major']			  = $row['major'];
	$info['business_name']    = $row['business_name'];
	$info['zipcode']    	  = $row['zipcode'];
	$info['rank_points']      = $row['rank_points'];
	
	if($info['user_type'] == 1)
	{
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id'";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_entered'] = count($count);;
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id' AND awarded=1 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_won'] = count($count);
		$info['totalEarn'] = 0.00;
		
		for($i=0; $i < count($count); $i++)
		{
			$info['totalEarn'] = $info['totalEarn'] + $count[$i]['approxPrice'];
		}
							
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('watchlist'). " WHERE user_id = '$user_id' ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_watched'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id' AND awarded=1 AND accepted=0 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['won_number'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id' AND awarded=1 AND review!='' ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['review_number'] = count($count);
		
	}
	else
	{
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects'). " WHERE uid = '$user_id' ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_created'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects'). " AS p JOIN ". $GLOBALS['ecs']->table('proposals'). " AS pp ". " WHERE p.uid = '$user_id' AND ( p.status = 2 OR p.status = 3 OR p.status = 4 OR p.status = 5) AND p.pid=pp.pid AND pp.awarded = 1 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_awarded'] = count($count);
		$info['totalEarn'] = 0.00;
		
		for($i=0; $i < count($count); $i++)
		{
			$info['totalEarn'] = $info['totalEarn'] + $count[$i]['approxPrice'];
		}
				
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects')." AS p JOIN ".$GLOBALS['ecs']->table('proposals')." AS pp ". " WHERE p.uid = '$user_id' AND pp.pid=p.pid AND awarded=0 AND accepted=0 AND watched=0 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['new_proposal_number'] = count($count);
		
		//$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects')." AS p JOIN ".$GLOBALS['ecs']->table('proposals')." AS pp ". " WHERE p.uid = '$user_id' AND pp.pid=p.pid AND awarded=1 AND accepted=1 AND review='' ";
		//$count = $GLOBALS['db']->getAll($sql);
		//$info['new_accept_number'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects')." AS p JOIN ".$GLOBALS['ecs']->table('proposals')." AS pp ". " WHERE p.uid = '$user_id' AND pp.pid=p.pid AND awarded=1 AND accepted=1 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['accept_number'] = count($count);
		
	}
	
    return $info;
}



/**
 * 获取用户中心默认页面所需的数据
 *
 * @access  public
 * @param   int         $user_id            用户ID
 *
 * @return  array       $info               默认页面所需资料数组
 */
function get_user_default($user_id)
{
	
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('users'). " AS u JOIN ". $GLOBALS['ecs']->table('gender'). " AS g JOIN ".$GLOBALS['ecs']->table('trainingservice'). " AS t JOIN ". $GLOBALS['ecs']->table('experience')." AS e "." WHERE user_id = '$user_id' AND u.sex = g.xid AND t.xid = u.training_service AND u.experience=e.xid ";
    $row = $GLOBALS['db']->getRow($sql);
    $info = array();
    $info['username']  = stripslashes($row['user_name']);
	$info['user_type']  = $row['user_type'];
	$info['user_id']  = $row['user_id'];
   
    //如果$_SESSION中时间无效说明用户是第一次登录。取当前登录时间。
    $last_time = !isset($_SESSION['last_time']) ? $row['last_login'] : $_SESSION['last_time'];

    if ($last_time == 0)
    {
        $_SESSION['last_time'] = $last_time = gmtime();
    }
	
    $info['last_time'] 		  = local_date($GLOBALS['_CFG']['time_format'], $last_time);
	$info['firstname'] 		  = $row['firstname'];
	$info['lastname']         = $row['lastname'];
	$info['user_money']		  = $row['user_money'];	
	$info['address'] 	      = $row['address'];
	$info['province'] 		  = $row['province'];
	$info['country'] 		  = $row['country'];
	$info['genderType'] 	  = $row['genderType'];
	$info['photo']            = $row['photo'];
	$info['credit_line']      = $row['credit_line'];
	$info['experienceType']   = $row['experienceType'];
	$info['training_specialties'] = $row['training_specialties'];
	$info['trainingServiceType'] = $row['trainingServiceType'];
	$info['credentials']	  = $row['credentials'];
	$info['BIO']              = $row['BIO'];
	$info['education']        = $row['education'];
	$info['school']			  = $row['school'];
	$info['major']			  = $row['major'];
	$info['business_name']    = $row['business_name'];
	$info['zipcode']    	  = $row['zipcode'];
	
	if($info['user_type'] == 1)
	{
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id'";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_entered'] = count($count);;
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id' AND awarded=1 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_won'] = count($count);
						
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('watchlist'). " WHERE user_id = '$user_id' ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_watched'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id' AND awarded=1 AND accepted=0 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['won_number'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposals'). " WHERE uid = '$user_id' AND awarded=1 AND review!='' ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['review_number'] = count($count);
		
	}
	else
	{
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects'). " WHERE uid = '$user_id' ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_created'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects'). " WHERE uid = '$user_id' AND ( status = 2 OR status = 3 OR status = 4 OR status = 5) ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['projects_awarded'] = count($count);
				
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects')." AS p JOIN ".$GLOBALS['ecs']->table('proposals')." AS pp ". " WHERE p.uid = '$user_id' AND pp.pid=p.pid AND awarded=0 AND accepted=0 AND watched=0 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['new_proposal_number'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects')." AS p JOIN ".$GLOBALS['ecs']->table('proposals')." AS pp ". " WHERE p.uid = '$user_id' AND pp.pid=p.pid AND awarded=1 AND accepted=1 AND review='' ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['new_accept_number'] = count($count);
		
		$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('projects')." AS p JOIN ".$GLOBALS['ecs']->table('proposals')." AS pp ". " WHERE p.uid = '$user_id' AND pp.pid=p.pid AND awarded=1 AND accepted=1 ";
		$count = $GLOBALS['db']->getAll($sql);
		$info['accept_number'] = count($count);
		
	}
	
    return $info;
}
?>